<?php
    /**
    * Witness view by ID
    * @author: trungdt
    * @since: Feb 2, 2011
    * @version: 1.0
    * 
    */
    if (!defined('EXPONENT')) exit('');

    /**
    * How many items per page
    * WARNING: Must implement better in module config
    */
    $max_item =15;
    // Administrator access restrict
    $lang=exponent_lang_loadFile("modules/demandmodule/actions/demand-manage.php");
    $lang2=exponent_lang_loadFile("modules/demandmodule/demand.php");

    // check permission
    $loc=null;
    $loc->mod="demandmodule";

    // get witness ID
    $id=-1;
    if (isset($_REQUEST['id']))
    {
        $id=$_REQUEST['id'];
        $temp=explode("-",$id);
        $id=intval($temp[0]);
    }

    // first, try to get witness item first
    $temp=$db->selectObject("witness","id = {$id}");
    if (($temp != null) && ($temp->demand_id > -1))
    {
        $temp=$db->selectObject("demands","id = {$temp->demand_id}");
        $params=array(
        'action'=>'demand_detail',
        'module'=>'demandmodule',
        'id'=>($temp->id."-"._to_vietnamese_with_nomark($temp->name))
        );
        $link=exponent_core_makeLink($params);
        $link.="#w{$id}";
        header("Location: {$link}");
        die();
    }
    else
    {
        // get page
        $page=1;
        $pages=1; 
        $start_pos=0;
        // end of pagination
        $fields_witness="{$db->prefix}witness.id, {$db->prefix}witness.name, {$db->prefix}witness.content, {$db->prefix}witness.user_id, {$db->prefix}witness.username, {$db->prefix}witness.post_date,
        {$db->prefix}witness.`status`, {$db->prefix}witness.unapprove_reason, {$db->prefix}witness.unapprove_username, {$db->prefix}witness.publish_date, 7 important_level, 2 `type`, {$db->prefix}subjects.name subject_name, 2 sendmail, 0 pray_count ";


        $user_id=-1;
        if (exponent_users_isLoggedIn())
        {
            $user_id = $user->id;
        }
        $where="{$db->prefix}witness.id = {$id} 
        AND ((status =2) OR ((status<2) AND (user_id = {$user_id})))";

        $data=$db->selectObjectsBySql("                
        SELECT {$fields_witness}
        FROM {$db->prefix}witness INNER JOIN {$db->prefix}subjects
        ON {$db->prefix}witness.subject_id = {$db->prefix}subjects.id
        WHERE ({$where}) 

        ORDER BY status ASC, post_date DESC, important_level DESC, publish_date DESC LIMIT {$start_pos},{$max_item}
        ");

        if (count($data)>0)
        {
            $show_unapprove=false;
            if (exponent_users_isLoggedIn() && 
            (exponent_permissions_check("administrate",$loc) || 
            exponent_permissions_check("approve_demand",$loc))
            )
            {
                $show_unapprove=true;
            }
            demandmodule::demand_witness_show($data, $lang, $lang2, $show_unapprove);

            $title=$lang2['witness-detail-title'];
            $page_action="witness-detail";

            $template=new template("demandmodule","_demand_manage");
            $template->assign("data",$data);
            $template->register_permissions(array('approve_demand',"administrate"),$loc);  
            $template->assign("page",$page);
            $template->assign("pages",$pages);
            $template->assign("title",$title);
            $template->assign("page_action",$page_action); 
            $template->assign("start_index",$start_pos);
            $template->assign("demand_manage",0);
            if (exponent_users_isLoggedIn())
            {
                $template->assign("logged",1);
            }
            _ab_theme_action_register_title($title." {$data[0]->name} - ".SITE_TITLE);
            $template->output();
        }
        else
        {
            echo SITE_404_HTML;
        }
    }
?>
